package org.itboys.mobile.service.mysql.web.product;


import org.apache.commons.lang3.StringUtils;
import org.itboys.mobile.dao.product.SundayProductCategoryFirstStandardMapper;
import org.itboys.mobile.dao.product.SundayProductCategorySecondStandardMapper;
import org.itboys.mobile.dto.product.SundayProductCategoryStandardDto;
import org.itboys.mobile.service.lasted.SundayMobileUploadService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;

import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mobile.entity.mysql.product.SundayProductCategoryFirstStandard;
import org.itboys.mobile.entity.mysql.product.SundayProductCategorySecondStandard;
import org.itboys.mongodb.service.BaseMySqlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_商品_类目_service_web
 * 当前为单规格。如果需要多规格，请自行扩展
 */

@Service
public class SundayProductCategoryService extends BaseMySqlService<SundayProductCategory> {
	@Autowired
	private SundayMobileUploadService uploadService;
	@Autowired
	private ResourceHolder resourceHolder;
	@Autowired
	private SundayProductCategoryFirstStandardMapper firstMapper;
	@Autowired
	private SundayProductCategorySecondStandardMapper secondMapper;

	/**
	 * 
	 * 获取商品类目的类目。树结构类型。
	 */
	public List<SundayProductCategory> getCategoryTreeJson(){
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("sort_name","sequence");
		param.put("sort_order","asc");
		List<SundayProductCategory> all= super.select(param);
		List<SundayProductCategory> parents = new ArrayList<SundayProductCategory>();
		List<SundayProductCategory> children = new ArrayList<SundayProductCategory>();

		for(SundayProductCategory c:all){
			if(StringUtils.isNotEmpty(c.getImage())){
				c.setImage(resourceHolder.getStringValue("imgRoot")+c.getImage());
			}else{
				c.setImage(null);
			}

			if(c.getParentId().longValue()==0L){
				parents.add(c);
			}else{
				children.add(c);
			}
		}
		//组装TREE
		for(SundayProductCategory parent:parents){
			List<SundayProductCategory> child = new ArrayList<SundayProductCategory>();
			for(SundayProductCategory c:children){
				if(LongUtil.isNotZreo(c.getParentId())&&
						c.getParentId().longValue()==parent.getId().longValue()){
					child.add(c);
				}
			}
			parent.setChildren(child);
		}
		return parents;
	}

	/**
	 * 新增或修改分类
	 * @param category
	 * @param imageFile
	 * @throws Exception
	 */
	public void save(SundayProductCategory category,MultipartFile imageFile) throws Exception{
		Map<String, String> map = uploadService.uploadSingle(imageFile);
		if(StringUtils.isNotEmpty(map.get(SundayMobileUploadService.UPLOAD_HALF_PATH))){
			category.setImage(map.get(SundayMobileUploadService.UPLOAD_HALF_PATH));
		}
		if(LongUtil.isNotZreo(category.getId())){
			if(LongUtil.isNull(category.getParentId())){
				category.setParentId(0L);
			}
			super.update(category);
		}else{
			super.insert(category);
		}
	}
	/**
	 * 删除商品类目
	 * @param categoryId
	 */
	public void deleteCategory(Long categoryId){
		super.delete(categoryId);
		logger.info("删除商品类目","id="+categoryId);
	}
	/**-
	 * -------------------------------规格部分--------------------
	 *
	 */
	//获取分类-规格-树形结构。含一级和二级规格
	public List<Map<String,Object>> getCategoryStandardTreeJson(Long categoryId){
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("categoryId",categoryId);
		//当前分类下所有的一级规格
		List<SundayProductCategoryFirstStandard> first_standards= firstMapper.select(param);
		//当前分类下所有的二级规格
		List<SundayProductCategorySecondStandard> second_standards= secondMapper.select(param);

		List<Map<String,Object>> parents = new ArrayList<Map<String,Object>>();
	//	List<Map<String,Object>> childrens = new ArrayList<Map<String,Object>>();
		//组装TREE
		for(SundayProductCategoryFirstStandard first_standard:first_standards){
			Map<String,Object> parent = new HashMap<String,Object>();
			parent.put("id",first_standard.getId());
			parent.put("type",1);
			parent.put("name",first_standard.getName());
			parent.put("categoryId",first_standard.getCategoryId());
			parent.put("categoryName",first_standard.getCategoryName());
			parent.put("firstId",0L);
			parent.put("firstName",null);
			List<Map<String,Object>> children = new ArrayList<Map<String,Object>>();

		//	List<SundayProductCategorySecondStandard> children = new ArrayList<SundayProductCategorySecondStandard>();
			for(SundayProductCategorySecondStandard second_standard:second_standards){
				if(		LongUtil.isNotZreo(first_standard.getCategoryId())&&
						LongUtil.isNotZreo(second_standard.getCategoryId())&&
						LongUtil.isNotZreo(second_standard.getFirstId())&&
						first_standard.getCategoryId().longValue()==second_standard.getCategoryId().longValue()&&
						first_standard.getId().longValue()==second_standard.getFirstId().longValue()
						){
					Map<String,Object> child = new HashMap<String,Object>();
					child.put("id",second_standard.getId());
					child.put("type",2);
					child.put("name",second_standard.getName());
					child.put("categoryId",second_standard.getCategoryId());
					child.put("categoryName",second_standard.getCategoryName());
					child.put("firstId",second_standard.getFirstId());
					child.put("firstName",second_standard.getFirstName());
					child.put("expanded",false);
					children.add(child);
				}
			}
			parent.put("children",children);
			parent.put("expanded",ListUtil.isNotNull(children)?true:false);

			parents.add(parent);
		}
		return parents;
	}

	/**
	 * 新增或修改规格（一级或二级规格）
	 * @param id
	 * @param name
	 * @param categoryId
	 * @param firstId
	 */
	public void saveStandard(Long id ,String name ,Long categoryId,Long firstId){
		//修改
		if(LongUtil.isNotZreo(id)){
			//修改二级规格
			if(LongUtil.isNotZreo(firstId)){
				secondMapper.update(new SundayProductCategorySecondStandard(id, name, categoryId, firstId));
				//修改一级规格
			}else{
				firstMapper.update(new SundayProductCategoryFirstStandard(id, name, categoryId));
			}
		//新增
		}else{
			//新增二级规格
			if(LongUtil.isNotZreo(firstId)){
				secondMapper.insert(new SundayProductCategorySecondStandard(0l, name, categoryId, firstId));
				//新增一级规格
			}else{
				Map<String,Object> param = new HashMap<String, Object>();
				param.put("categoryId", categoryId);
				Serializable firsts = firstMapper.count(param);
				if(firsts!=null&&Long.valueOf(firsts.toString())>=4){
					throw new ErrorException("单个分类的一级规格，最大不能超过4组");
				}
				firstMapper.insert(new SundayProductCategoryFirstStandard(0l, name, categoryId));
			}
		}
	}

	/**
	 *删除规格
	 * @param id
	 * @param type 1一级规格，2二级规格
	 */
	public void deleteStandard(Long id,Integer type){
		if(type==1){
			firstMapper.delete(id);
		}
		if(type==2){
			secondMapper.delete(id);
		}
	}

	//获取一个分类所有下一级和二级规格。并且勾选商品自己有的规格。新增或编辑商品时使用
	public List<SundayProductCategoryStandardDto> getStandardByCategoryIdAndProductId(Long categoryId, Long productId){
		//返回的结果
		List<SundayProductCategoryStandardDto> parents = new ArrayList<SundayProductCategoryStandardDto>();
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("categoryId", categoryId);
		//获取分类的一级规格
		List<SundayProductCategoryFirstStandard> firsts= firstMapper.select(param);

		//获取分类的二级规格
		List<SundayProductCategorySecondStandard> seconds= secondMapper.select(param);
		//将分类的一级和二级规格转换为DTO，且具有父子关系
		for(SundayProductCategoryFirstStandard first:firsts){
			SundayProductCategoryStandardDto parent = new SundayProductCategoryStandardDto();
			parent.setId(first.getId());
			parent.setName(first.getName());
			List<SundayProductCategoryStandardDto> children = new ArrayList<SundayProductCategoryStandardDto>();
			for(SundayProductCategorySecondStandard second:seconds){
				if(LongUtil.isNotZreo(second.getFirstId())&&
						second.getFirstId().longValue()==first.getId().longValue()){
					SundayProductCategoryStandardDto child = new SundayProductCategoryStandardDto();
					child.setId(second.getId());
					child.setName(second.getName());
					children.add(child);
					//判断子节点是否被勾选过。
				}

			}
			parent.setChildren(children);
			parents.add(parent);
		}
		return parents;
	}
}
